package com.happyscan138.makefun.adapter;

import android.content.Context;
import android.content.Intent;
import android.content.res.Configuration;
import android.media.MediaPlayer;
import android.os.Environment;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AbsListView;
import android.widget.BaseAdapter;
import android.widget.FrameLayout;
import android.widget.ImageButton;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;

import com.happyscan138.makefun.BaseAdapter.main_hot_listview;
import com.happyscan138.makefun.R;
import com.happyscan138.makefun.Urls.Urls;
import com.happyscan138.makefun.VideoviewPlay;
import com.happyscan138.makefun.View.RoundImageView;
import com.happyscan138.makefun.base.VideoPlayView;
import com.happyscan138.makefun.base.http;
import com.happyscan138.makefun.more_myself;
import com.lidroid.xutils.HttpUtils;
import com.lidroid.xutils.http.ResponseInfo;
import com.lidroid.xutils.http.callback.RequestCallBack;
import com.loopj.android.http.AsyncHttpClient;
import com.loopj.android.http.JsonHttpResponseHandler;
import com.loopj.android.http.RequestHandle;
import com.loopj.android.http.RequestParams;
import com.squareup.picasso.Picasso;

import org.json.JSONException;
import org.json.JSONObject;

import java.io.File;
import java.util.List;

import cz.msebera.android.httpclient.Header;
import pl.droidsonroids.gif.GifDrawable;
import pl.droidsonroids.gif.GifImageView;

import static com.happyscan138.makefun.base.http.client;
import static com.happyscan138.makefun.base.http.params;
import static com.loopj.android.http.AsyncHttpClient.log;

/**
 * Created by Administrator on 2016/12/26.
 */

public class main_hot_adapter extends BaseAdapter implements VideoPlayView.MediaPlayerImpl,AbsListView.OnScrollListener{
    public main_hot_adapter() {

    }

    public main_hot_adapter(Context mcontext, List<main_hot_listview> listItem, String access_key, ListView MainNewListview ) {
        this.mcontext = mcontext;
        this.listItem = listItem;
        this.MainNewListview=MainNewListview;
        this.access_key=access_key;

    }
    private ListView MainNewListview;
    private MediaPlayer mediaPlayer;
    private Context mcontext;
    private List<main_hot_listview> listItem;
    private String access_key;
    private String userID;
    private  boolean isSelected;
    private main_hot_adapter adapter;
    private static final int TYPE_NULL = 0;
    private static final int TYPE_IMAGE = 1;
    private static final int TYPE_VOICE = 2;
    private static final int TYPE_MOVIE = 3;
    private static final int TYPE_GIF = 4;
    private boolean isPlaying;
    private View currentItemView;
    private int currentPosition = -1;
    private VideoPlayView playView;
    @Override
    public int getCount() {
        return listItem.size();
    }
    //多布局的核心，通过这个判断类别
    @Override
    public int getItemViewType(int position) {
        if(listItem.get(position).resource.equals(Urls.base)){
            if(listItem.get(position).m3u8.equals(Urls.base)){
                return TYPE_NULL;
            }else {
                return TYPE_MOVIE;
            }

        }else {
            if (listItem.get(position).resource.endsWith(".jpg") || listItem.get(position).resource.endsWith(".png")) {
                return TYPE_IMAGE;
            } else if (listItem.get(position).resource.endsWith(".mp3")) {
                return TYPE_VOICE;
            } else if (listItem.get(position).resource.endsWith(".gif")) {
                return TYPE_GIF;
            }else  if(listItem.get(position).resource.endsWith(".mp4")){
                return TYPE_MOVIE;
            }
        }
        return TYPE_NULL;
    }
    //类别数目
    @Override
    public int getViewTypeCount() {
        return 5;
    }

    @Override
    public View getView(final int position, View view, ViewGroup viewGroup) {
        int Type = getItemViewType(position);

        //要加载的布局
        ViewHolder_dynamic_IMAGE viewHolder_Image = null;
        ViewHolder_Dynamic_NULL viewHolder_null = null;
        ViewHolder_Dynamic_VOICE viewHolder_Voice=null;
        ViewHolder_Dynamic_MOVIE viewHolder_movie =null;
        ViewHolder_Dynamic_GIF  viewHolder_gif =null;
        //判断converView是否为空
        if (view == null) {
            switch (Type) {
                case TYPE_IMAGE:

                    viewHolder_Image = new ViewHolder_dynamic_IMAGE();
                    view = LayoutInflater.from(mcontext).inflate(R.layout.dynamic_image, viewGroup, false);
                    viewHolder_Image.Dynamic_IMAGE_head = (RoundImageView) view.findViewById(R.id.dynamic_image_head);
                    viewHolder_Image.Dynamic_IMAGE_name = (TextView) view.findViewById(R.id.dynamic_image_name);
                    viewHolder_Image.Dynamic_IMAGE_time = (TextView) view.findViewById(R.id.dynamic_image_time);
                    viewHolder_Image.Dynamic_IMAGE_notice = (ImageButton) view.findViewById(R.id.dynamic_image_notice_button);
                    viewHolder_Image.Dynamic_IMAGE_text = (TextView) view.findViewById(R.id.dynamic_image_text);
                    viewHolder_Image.Dynamic_IMAGE_image = (ImageView) view.findViewById(R.id.dynamic_image_iamge);
                    viewHolder_Image.Dynamic_IMAGE_viewNum = (TextView) view.findViewById(R.id.dynamic_image_viewNum);
                    viewHolder_Image.Dynamic_IMAGE_answerNum = (TextView) view.findViewById(R.id.dynamic_image_answerNum);
                    viewHolder_Image.Dynamic_IMAGE_commentNum = (TextView) view.findViewById(R.id.dynamic_image_commentNum);
                    viewHolder_Image.Dynamic_IMAGE_goodNum = (TextView) view.findViewById(R.id.dynamic_image_goodNum);
                    //将viewHolder与converView绑定
                    view.setTag(R.id.Tag_Image, viewHolder_Image);
                    break;
                case TYPE_NULL:
                    viewHolder_null = new ViewHolder_Dynamic_NULL();
                    view = LayoutInflater.from(mcontext).inflate(R.layout.dynamic_null, viewGroup, false);
                    viewHolder_null.Dynamic_NULL_head = (RoundImageView) view.findViewById(R.id.main_new_head);
                    viewHolder_null.Dynamic_NULL_name = (TextView) view.findViewById(R.id.main_new_name);
                    viewHolder_null.Dynamic_NULL_time = (TextView) view.findViewById(R.id.main_new_time);
                    viewHolder_null.Dynamic_NULL_notice = (ImageButton) view.findViewById(R.id.main_new_item_notice_button);
                    viewHolder_null.Dynamic_NULL_text = (TextView) view.findViewById(R.id.main_new_text);
                    viewHolder_null.Dynamic_NULL_viewNum = (TextView) view.findViewById(R.id.main_new_viewNum);
                    viewHolder_null.Dynamic_NULL_answerNum = (TextView) view.findViewById(R.id.main_new_answerNum);
                    viewHolder_null.Dynamic_NULL_commentNum = (TextView) view.findViewById(R.id.main_new_commentNum);
                    viewHolder_null.Dynamic_NULL_goodNum = (TextView) view.findViewById(R.id.main_new_goodNum);
                    view.setTag(R.id.Tag_Null, viewHolder_null);
                    break;
                case TYPE_VOICE:
                    viewHolder_Voice=new ViewHolder_Dynamic_VOICE();
                    view = LayoutInflater.from(mcontext).inflate(R.layout.dynamic_voice, viewGroup, false);
                    viewHolder_Voice.Dynamic_VOICE_head = (RoundImageView) view.findViewById(R.id.dynamic_voice_head);
                    viewHolder_Voice.Dynamic_VOICE_name = (TextView) view.findViewById(R.id.dynamic_voice_name);
                    viewHolder_Voice.Dynamic_VOICE_time = (TextView) view.findViewById(R.id.dynamic_voice_time);
                    viewHolder_Voice.Dynamic_VOICE_notice = (ImageButton) view.findViewById(R.id.dynamic_voice_notice_button);
                    viewHolder_Voice.Dynamic_VOICE_text = (TextView) view.findViewById(R.id.dynamic_voice_text);
                    viewHolder_Voice.Dynamic_VOICE_Play = (ImageButton) view.findViewById(R.id.dynamic_voice_play_voice);

                    viewHolder_Voice.Dynamic_VOICE_Play.setOnClickListener(new View.OnClickListener() {
                        @Override
                        public void onClick(View v) {
                            //播放音频
                            mediaPlayer=new MediaPlayer();
                            try {
                                mediaPlayer.setDataSource(listItem.get(position).resource);
                                log.e("voice",listItem.get(position).resource);
                                mediaPlayer.prepare();
                            } catch (Exception e) {
                                e.printStackTrace();
                            }
                            mediaPlayer.start();
                        }
                    });
                    viewHolder_Voice.Dynamic_VOICE_viewNum = (TextView) view.findViewById(R.id.dynamic_voice_viewNum);
                    viewHolder_Voice.Dynamic_VOICE_answerNum = (TextView) view.findViewById(R.id.dynamic_voice_answerNum);
                    viewHolder_Voice.Dynamic_VOICE_commentNum = (TextView) view.findViewById(R.id.dynamic_voice_commentNum);
                    viewHolder_Voice.Dynamic_VOICE_goodNum = (TextView) view.findViewById(R.id.dynamic_voice_goodNum);
                    view.setTag(R.id.Tag_Voice, viewHolder_Voice);
                    break;
                case TYPE_MOVIE:
                    viewHolder_movie=new ViewHolder_Dynamic_MOVIE();
                    view = LayoutInflater.from(mcontext).inflate(R.layout.dynamic_movie, viewGroup, false);
                    viewHolder_movie.Dynamic_MOVIE_head = (RoundImageView) view.findViewById(R.id.dynamic_movie_head);
                    viewHolder_movie.Dynamic_MOVIE_name = (TextView) view.findViewById(R.id.dynamic_movie_name);
                    viewHolder_movie.Dynamic_MOVIE_time = (TextView) view.findViewById(R.id.dynamic_movie_time);
                    viewHolder_movie.Dynamic_MOVIE_notice = (ImageButton) view.findViewById(R.id.dynamic_movie_notice_button);
                    viewHolder_movie.Dynamic_MOVIE_text = (TextView) view.findViewById(R.id.dynamic_movie_text);
                    viewHolder_movie.Dynamic_MOVI_Play_btn = (ImageView) view.findViewById(R.id.dynamic_movie_play_btn);
                    viewHolder_movie.Dynamic_MOVI_Show_layout = (FrameLayout) view.findViewById(R.id.dynamic_movie_show_layout);
                    viewHolder_movie.Dynamic_MOVI_Play_view = (VideoPlayView) view.findViewById(R.id.dynamic_movie_video_play_view);
                    viewHolder_movie.Dynamic_MOVI_Img=(ImageView)view.findViewById(R.id.dynamic_video_img);
                    viewHolder_movie.Dynamic_MOVIE_viewNum = (TextView) view.findViewById(R.id.dynamic_movie_viewNum);
                    viewHolder_movie.Dynamic_MOVIE_answerNum = (TextView) view.findViewById(R.id.dynamic_movie_answerNum);
                    viewHolder_movie.Dynamic_MOVIE_commentNum = (TextView) view.findViewById(R.id.dynamic_movie_commentNum);
                    viewHolder_movie.Dynamic_MOVIE_goodNum = (TextView) view.findViewById(R.id.dynamic_movie_goodNum);
                    view.setTag(R.id.Tag_Moive, viewHolder_movie);
                    break;
                case TYPE_GIF:
                    viewHolder_gif =new ViewHolder_Dynamic_GIF();
                    view = LayoutInflater.from(mcontext).inflate(R.layout.dynamic_gif, viewGroup, false);
                    viewHolder_gif.Dynamic_GIF_head=(RoundImageView)view.findViewById(R.id.dynamic_gif_head);
                    viewHolder_gif.Dynamic_GIF_name=(TextView) view.findViewById(R.id.dynamic_gif_name);
                    viewHolder_gif.Dynamic_GIF_time = (TextView) view.findViewById(R.id.dynamic_gif_time);
                    viewHolder_gif.Dynamic_GIF_notice = (ImageButton) view.findViewById(R.id.dynamic_gif_notice_button);
                    viewHolder_gif.Dynamic_GIF_text = (TextView) view.findViewById(R.id.dynamic_gif_text);
                    viewHolder_gif.Dynamic_GIF_image = (GifImageView) view.findViewById(R.id.dynamic_gif);
                    viewHolder_gif.Dynamic_GIF_viewNum = (TextView) view.findViewById(R.id.dynamic_gif_viewNum);
                    viewHolder_gif.Dynamic_GIF_answerNum = (TextView) view.findViewById(R.id.dynamic_gif_answerNum);
                    viewHolder_gif.Dynamic_GIF_commentNum = (TextView) view.findViewById(R.id.dynamic_gif_commentNum);
                    viewHolder_gif.Dynamic_GIF_goodNum = (TextView) view.findViewById(R.id.dynamic_gif_goodNum);
                    view.setTag(R.id.Tag_Gif, viewHolder_gif);
                    break;
            }
        } else {
            //如果存在converView，取出保存在converView中的viewHolder
            switch (Type) {
                case TYPE_IMAGE:
                    viewHolder_Image = (ViewHolder_dynamic_IMAGE) view.getTag(R.id.Tag_Image);
                    break;
                case TYPE_NULL:
                    viewHolder_null = (ViewHolder_Dynamic_NULL) view.getTag(R.id.Tag_Null);
                    break;
                case TYPE_VOICE:
                    viewHolder_Voice=(ViewHolder_Dynamic_VOICE)view.getTag(R.id.Tag_Voice);
                    break;
                case TYPE_MOVIE:
                    viewHolder_movie=(ViewHolder_Dynamic_MOVIE)view.getTag(R.id.Tag_Moive);
                    break;
                case TYPE_GIF:
                    viewHolder_gif=(ViewHolder_Dynamic_GIF)view.getTag(R.id.Tag_Gif);
                    break;
            }
        }
        main_hot_listview bean = listItem.get(position);
        switch (Type) {
            case TYPE_IMAGE:
                if(bean!=null) {
                    userID = bean.userID;
                    //通过viewHolder找到对应控件，避免每次通过findViewById找控件
                    Picasso.with(mcontext).load(bean.icon).into(viewHolder_Image.Dynamic_IMAGE_head);
                    viewHolder_Image.Dynamic_IMAGE_head.setOnClickListener(new View.OnClickListener() {
                        @Override
                        public void onClick(View v) {
                            Intent intent = new Intent();
                            intent.putExtra("infoUserID",listItem.get(position).userID);
                            Log.e("infoUserIDImgClick", listItem.get(position).userID);
                            intent.setClass(mcontext, more_myself.class);
                            mcontext.startActivity(intent);
                        }
                    });
                    viewHolder_Image.Dynamic_IMAGE_name.setText(bean.name);
                    log.e("bean.time", bean.time);
                    viewHolder_Image.Dynamic_IMAGE_time.setText(bean.time);
                    final ViewHolder_dynamic_IMAGE finalViewHolder_Image = viewHolder_Image;
                    viewHolder_Image.Dynamic_IMAGE_notice.setOnClickListener(new View.OnClickListener() {
                        @Override
                        public void onClick(View v) {
                            Log.e("buttonnotice", "sdsdsd");
                            isSelected = !isSelected;
                            finalViewHolder_Image.Dynamic_IMAGE_notice.setBackgroundResource(isSelected ? R.mipmap.benotice : R.mipmap.notic);
                            client = new AsyncHttpClient();
                            params = new RequestParams();
                            params.put("access_key", access_key);
                            log.e("access_key", access_key);
                            params.put("focus_u_id", userID);
                            String a = userID + "";
                            log.e("focus_u_id", a);
                            log.e("params", params.toString());
                            RequestHandle handle = client.post(Urls.Notice, params, new JsonHttpResponseHandler() {
                                @Override
                                public void onSuccess(int statusCode, Header[] headers, JSONObject response) {
                                    super.onSuccess(statusCode, headers, response);
                                    log.e("onSuccess", response.toString());
                                    String jsonstr = response.toString();
                                    try {
                                        JSONObject jsonObject = new JSONObject(jsonstr);
                                        String msg = jsonObject.getString("msg");
                                        Toast.makeText(mcontext, msg, Toast.LENGTH_LONG).show();
                                        String code = jsonObject.getString("code");
                                        int code_int = Integer.valueOf(code).intValue();
                                        if (code_int == 163) {
                                            finalViewHolder_Image.Dynamic_IMAGE_notice.setBackgroundResource(R.mipmap.benotice);
                                        } else {
                                            finalViewHolder_Image.Dynamic_IMAGE_notice.setBackgroundResource(R.mipmap.notic);
                                        }
                                    } catch (JSONException e) {
                                        e.printStackTrace();
                                    }
                                }

                                @Override
                                public void onFailure(int statusCode, Header[] headers, String responseString, Throwable throwable) {
                                    super.onFailure(statusCode, headers, responseString, throwable);
                                    log.e("onFailure", responseString);
                                }
                            });
                        }
                    });
                    viewHolder_Image.Dynamic_IMAGE_text.setText(bean.text);
                    Picasso.with(mcontext).load(bean.resource).into(viewHolder_Image.Dynamic_IMAGE_image);
                 //   imageHelper=new ImageHelper(mcontext).display(viewHolder_Image.Dynamic_IMAGE_image,bean.resource);
                    viewHolder_Image.Dynamic_IMAGE_viewNum.setText(bean.viewNum + "");
                    viewHolder_Image.Dynamic_IMAGE_answerNum.setText(bean.answerNum + "");
                    viewHolder_Image.Dynamic_IMAGE_commentNum.setText(bean.commentNum + "");
                    viewHolder_Image.Dynamic_IMAGE_goodNum.setText(bean.goodNum + "");
                }
                break;
            case TYPE_NULL:
                if(bean!=null) {
                    userID = bean.userID;
                    //通过viewHolder找到对应控件，避免每次通过findViewById找控件
                    Picasso.with(mcontext).load(bean.icon).into(viewHolder_null.Dynamic_NULL_head);
                    viewHolder_null.Dynamic_NULL_head.setOnClickListener(new View.OnClickListener() {
                        @Override
                        public void onClick(View v) {
                            Intent intent = new Intent();
                            intent.putExtra("infoUserID",listItem.get(position).userID);
                            Log.e("infoUserIDImgClick", listItem.get(position).userID);
                            intent.setClass(mcontext, more_myself.class);
                            mcontext.startActivity(intent);
                        }
                    });
                    viewHolder_null.Dynamic_NULL_name.setText(bean.name);
                    log.e("bean.time", bean.time);
                    viewHolder_null.Dynamic_NULL_time.setText(bean.time);
                    final ViewHolder_Dynamic_NULL finalViewHolder_null = viewHolder_null;
                    viewHolder_null.Dynamic_NULL_notice.setOnClickListener(new View.OnClickListener() {
                        @Override
                        public void onClick(View v) {
                            Log.e("buttonnotice", "sdsdsd");
                            isSelected = !isSelected;
                            finalViewHolder_null.Dynamic_NULL_notice.setBackgroundResource(isSelected ? R.mipmap.benotice : R.mipmap.notic);
                            client = new AsyncHttpClient();
                            params = new RequestParams();
                            params.put("access_key", access_key);
                            log.e("access_key", access_key);
                            params.put("focus_u_id", userID);
                            String a = userID + "";
                            log.e("focus_u_id", a);
                            log.e("params", params.toString());
                            RequestHandle handle = client.post(Urls.Notice, params, new JsonHttpResponseHandler() {
                                @Override
                                public void onSuccess(int statusCode, Header[] headers, JSONObject response) {
                                    super.onSuccess(statusCode, headers, response);
                                    log.e("onSuccess", response.toString());
                                    String jsonstr = response.toString();
                                    try {
                                        JSONObject jsonObject = new JSONObject(jsonstr);
                                        String msg = jsonObject.getString("msg");
                                        Toast.makeText(mcontext, msg, Toast.LENGTH_LONG).show();
                                        String code = jsonObject.getString("code");
                                        int code_int = Integer.valueOf(code).intValue();
                                        if (code_int == 163) {
                                            finalViewHolder_null.Dynamic_NULL_notice.setBackgroundResource(R.mipmap.benotice);
                                        } else {
                                            finalViewHolder_null.Dynamic_NULL_notice.setBackgroundResource(R.mipmap.notic);
                                        }
                                    } catch (JSONException e) {
                                        e.printStackTrace();
                                    }
                                }

                                @Override
                                public void onFailure(int statusCode, Header[] headers, String responseString, Throwable throwable) {
                                    super.onFailure(statusCode, headers, responseString, throwable);
                                    log.e("onFailure", responseString);
                                }
                            });
                        }
                    });
                    viewHolder_null.Dynamic_NULL_text.setText(bean.text);
                    viewHolder_null.Dynamic_NULL_viewNum.setText(bean.viewNum + "");
                    viewHolder_null.Dynamic_NULL_answerNum.setText(bean.answerNum + "");
                    viewHolder_null.Dynamic_NULL_commentNum.setText(bean.commentNum + "");
                    viewHolder_null.Dynamic_NULL_goodNum.setText(bean.goodNum + "");
                }
                break;
            case TYPE_VOICE:
                if(bean!=null) {
                    userID = bean.userID;
                    //通过viewHolder找到对应控件，避免每次通过findViewById找控件
                    Picasso.with(mcontext).load(bean.icon).into(viewHolder_Voice.Dynamic_VOICE_head);
                    viewHolder_Voice.Dynamic_VOICE_head.setOnClickListener(new View.OnClickListener() {
                        @Override
                        public void onClick(View v) {
                            Intent intent = new Intent();
                            intent.putExtra("infoUserID",listItem.get(position).userID);
                            Log.e("infoUserIDImgClick", listItem.get(position).userID);
                            intent.setClass(mcontext, more_myself.class);
                            mcontext.startActivity(intent);
                        }
                    });
                    viewHolder_Voice.Dynamic_VOICE_name.setText(bean.name);
                    log.e("bean.time", bean.time);
                    viewHolder_Voice.Dynamic_VOICE_time.setText(bean.time);
                    final ViewHolder_Dynamic_VOICE finalViewHolder_Voice = viewHolder_Voice;
                    viewHolder_Voice.Dynamic_VOICE_notice.setOnClickListener(new View.OnClickListener() {
                        @Override
                        public void onClick(View v) {
                            Log.e("buttonnotice", "sdsdsd");
                            isSelected = !isSelected;
                            finalViewHolder_Voice.Dynamic_VOICE_notice.setBackgroundResource(isSelected ? R.mipmap.benotice : R.mipmap.notic);
                            client = new AsyncHttpClient();
                            params = new RequestParams();
                            params.put("access_key", access_key);
                            log.e("access_key", access_key);
                            params.put("focus_u_id", userID);
                            String a = userID + "";
                            log.e("focus_u_id", a);
                            log.e("params", params.toString());
                            RequestHandle handle = client.post(Urls.Notice, params, new JsonHttpResponseHandler() {
                                @Override
                                public void onSuccess(int statusCode, Header[] headers, JSONObject response) {
                                    super.onSuccess(statusCode, headers, response);
                                    log.e("onSuccess", response.toString());
                                    String jsonstr = response.toString();
                                    try {
                                        JSONObject jsonObject = new JSONObject(jsonstr);
                                        String msg = jsonObject.getString("msg");
                                        Toast.makeText(mcontext, msg, Toast.LENGTH_LONG).show();
                                        String code = jsonObject.getString("code");
                                        int code_int = Integer.valueOf(code).intValue();
                                        if (code_int == 163) {
                                            finalViewHolder_Voice.Dynamic_VOICE_notice.setBackgroundResource(R.mipmap.benotice);
                                        } else {
                                            finalViewHolder_Voice.Dynamic_VOICE_notice.setBackgroundResource(R.mipmap.notic);
                                        }
                                    } catch (JSONException e) {
                                        e.printStackTrace();
                                    }
                                }

                                @Override
                                public void onFailure(int statusCode, Header[] headers, String responseString, Throwable throwable) {
                                    super.onFailure(statusCode, headers, responseString, throwable);
                                    log.e("onFailure", responseString);
                                }
                            });
                        }
                    });
                    viewHolder_Voice.Dynamic_VOICE_text.setText(bean.text);
                    viewHolder_Voice.Dynamic_VOICE_viewNum.setText(bean.viewNum + "");
                    viewHolder_Voice.Dynamic_VOICE_answerNum.setText(bean.answerNum + "");
                    viewHolder_Voice.Dynamic_VOICE_commentNum.setText(bean.commentNum + "");
                    viewHolder_Voice.Dynamic_VOICE_goodNum.setText(bean.goodNum + "");
                }
                break;

            case TYPE_MOVIE:
                if(bean!=null) {
                    userID = bean.userID;
                    //通过viewHolder找到对应控件，避免每次通过findViewById找控件
                    Picasso.with(mcontext).load(bean.icon).into(viewHolder_movie.Dynamic_MOVIE_head);
                    viewHolder_movie.Dynamic_MOVIE_head.setOnClickListener(new View.OnClickListener() {
                        @Override
                        public void onClick(View v) {
                            Intent intent = new Intent();
                            intent.putExtra("infoUserID",listItem.get(position).userID);
                            Log.e("infoUserIDImgClick", listItem.get(position).userID);
                            intent.setClass(mcontext, more_myself.class);
                            mcontext.startActivity(intent);
                        }
                    });
                    viewHolder_movie.Dynamic_MOVIE_name.setText(bean.name);
                    log.e("bean.time", bean.time);
                    viewHolder_movie.Dynamic_MOVIE_time.setText(bean.time);
                    final ViewHolder_Dynamic_MOVIE finalViewHolder_Movie = viewHolder_movie;
                    viewHolder_movie.Dynamic_MOVIE_notice.setOnClickListener(new View.OnClickListener() {
                        @Override
                        public void onClick(View v) {
                            Log.e("buttonnotice", "sdsdsd");
                            isSelected = !isSelected;
                            finalViewHolder_Movie.Dynamic_MOVIE_notice.setBackgroundResource(isSelected ? R.mipmap.benotice : R.mipmap.notic);
                            client = new AsyncHttpClient();
                            params = new RequestParams();
                            params.put("access_key", access_key);
                            log.e("access_key", access_key);
                            params.put("focus_u_id", userID);
                            String a = userID + "";
                            log.e("focus_u_id", a);
                            log.e("params", params.toString());
                            RequestHandle handle = client.post(Urls.Notice, params, new JsonHttpResponseHandler() {
                                @Override
                                public void onSuccess(int statusCode, Header[] headers, JSONObject response) {
                                    super.onSuccess(statusCode, headers, response);
                                    log.e("onSuccess", response.toString());
                                    String jsonstr = response.toString();
                                    try {
                                        JSONObject jsonObject = new JSONObject(jsonstr);
                                        String msg = jsonObject.getString("msg");
                                        Toast.makeText(mcontext, msg, Toast.LENGTH_LONG).show();
                                        String code = jsonObject.getString("code");
                                        int code_int = Integer.valueOf(code).intValue();
                                        if (code_int == 163) {
                                            finalViewHolder_Movie.Dynamic_MOVIE_notice.setBackgroundResource(R.mipmap.benotice);
                                        } else {
                                            finalViewHolder_Movie.Dynamic_MOVIE_notice.setBackgroundResource(R.mipmap.notic);
                                        }
                                    } catch (JSONException e) {
                                        e.printStackTrace();
                                    }
                                }

                                @Override
                                public void onFailure(int statusCode, Header[] headers, String responseString, Throwable throwable) {
                                    super.onFailure(statusCode, headers, responseString, throwable);
                                    log.e("onFailure", responseString);
                                }
                            });
                        }
                    });
                    viewHolder_movie.Dynamic_MOVIE_text.setText(bean.text);
                    log.e("bean.Thumb", bean.Thumb);
                    if(bean.Thumb.equals(Urls.base)){
                        viewHolder_movie.Dynamic_MOVI_Img.setImageDrawable(mcontext.getResources().getDrawable(R.mipmap.makefun));
                    }else {
                        Picasso.with(mcontext).load(bean.Thumb).into(viewHolder_movie.Dynamic_MOVI_Img);
                    }
                    viewHolder_movie.Dynamic_MOVI_Play_btn.setOnClickListener(new View.OnClickListener() {
                        @Override
                        public void onClick(View v) {
                            Intent intent=new Intent();
                            intent.putExtra("url",listItem.get(position).resource);
                            intent.putExtra("Thumb",listItem.get(position).Thumb);
                            intent.putExtra("u3m8",listItem.get(position).m3u8);
                            intent.setClass(mcontext, VideoviewPlay.class);
                            mcontext.startActivity(intent);
                        }
                    });
                    if (currentPosition == position) {
                        viewHolder_movie.Dynamic_MOVI_Play_view.setVisibility(View.VISIBLE);

                    } else {
                        viewHolder_movie.Dynamic_MOVI_Play_view.setVisibility(View.GONE);
                        viewHolder_movie.Dynamic_MOVI_Show_layout.setVisibility(View.VISIBLE);
                        viewHolder_movie.Dynamic_MOVI_Play_view.stop();
                    }
                    viewHolder_movie.Dynamic_MOVIE_viewNum.setText(bean.viewNum + "");
                    viewHolder_movie.Dynamic_MOVIE_answerNum.setText(bean.answerNum + "");
                    viewHolder_movie.Dynamic_MOVIE_commentNum.setText(bean.commentNum + "");
                    viewHolder_movie.Dynamic_MOVIE_goodNum.setText(bean.goodNum + "");
                }
                break;
            case TYPE_GIF:
                if(bean!=null) {
                    userID = bean.userID;
                    //通过viewHolder找到对应控件，避免每次通过findViewById找控件
                    Picasso.with(mcontext).load(bean.icon).into(viewHolder_gif.Dynamic_GIF_head);
                    viewHolder_gif.Dynamic_GIF_head.setOnClickListener(new View.OnClickListener() {
                        @Override
                        public void onClick(View v) {
                            Intent intent = new Intent();
                            intent.putExtra("infoUserID",listItem.get(position).userID);
                            Log.e("infoUserIDImgClick", listItem.get(position).userID);
                            intent.setClass(mcontext, more_myself.class);
                            mcontext.startActivity(intent);
                        }
                    });
                    viewHolder_gif.Dynamic_GIF_name.setText(bean.name);
                    log.e("bean.time", bean.time);
                    viewHolder_gif.Dynamic_GIF_time.setText(bean.time);
                    final ViewHolder_Dynamic_GIF finalViewHolder_GIF = viewHolder_gif;
                    viewHolder_gif.Dynamic_GIF_notice.setOnClickListener(new View.OnClickListener() {
                        @Override
                        public void onClick(View v) {
                            isSelected = !isSelected;
                            finalViewHolder_GIF.Dynamic_GIF_notice.setBackgroundResource(isSelected ? R.mipmap.benotice : R.mipmap.notic);
                            http.client = new AsyncHttpClient();
                            http.params = new RequestParams();
                            http.params.put("access_key", access_key);
                            log.e("access_key", access_key);
                            http.params.put("focus_u_id", userID);
                            String a = userID + "";
                            log.e("focus_u_id", a);
                            log.e("params", http.params.toString());
                            RequestHandle handle = http.client.post(Urls.Notice, http.params, new JsonHttpResponseHandler() {
                                @Override
                                public void onSuccess(int statusCode, Header[] headers, JSONObject response) {
                                    super.onSuccess(statusCode, headers, response);
                                    log.e("onSuccess", response.toString());
                                    String jsonstr = response.toString();
                                    try {
                                        JSONObject jsonObject = new JSONObject(jsonstr);
                                        String msg = jsonObject.getString("msg");
                                        Toast.makeText(mcontext, msg, Toast.LENGTH_LONG).show();
                                        String code = jsonObject.getString("code");
                                        int code_int = Integer.valueOf(code).intValue();
                                        if (code_int == 163) {
                                            finalViewHolder_GIF.Dynamic_GIF_notice.setBackgroundResource(R.mipmap.benotice);
                                        } else {
                                            finalViewHolder_GIF.Dynamic_GIF_notice.setBackgroundResource(R.mipmap.notic);
                                        }
                                    } catch (JSONException e) {
                                        e.printStackTrace();
                                    }
                                }

                                @Override
                                public void onFailure(int statusCode, Header[] headers, String responseString, Throwable throwable) {
                                    super.onFailure(statusCode, headers, responseString, throwable);
                                    log.e("onFailure", responseString);
                                }
                            });
                        }
                    });
                    viewHolder_gif.Dynamic_GIF_text.setText(bean.text);

                    File saveImgPath = getImageDir(mcontext);
                    File gifSavePath1 = new File(saveImgPath, "gif1");
                    displayImage(bean.resource, gifSavePath1, viewHolder_gif.Dynamic_GIF_image);
                    viewHolder_gif.Dynamic_GIF_viewNum.setText(bean.viewNum + "");
                    viewHolder_gif.Dynamic_GIF_answerNum.setText(bean.answerNum + "");
                    viewHolder_gif.Dynamic_GIF_commentNum.setText(bean.commentNum + "");
                    viewHolder_gif.Dynamic_GIF_goodNum.setText(bean.goodNum + "");
                }
                break;
        }
        return view;
    }



    @Override
    public Object getItem(int i) {
        return null;
    }

    @Override
    public long getItemId(int i) {
        return 0;
    }
    //通过 set 方法在适配器中拿到这三个参数
    public void setPlayView(VideoPlayView playView) {
        this.playView = playView;
        playView.setMediaPlayerListenr(this);
        Log.i("XX", currentPosition + "");
        int[] curr = new int[2];
        currentItemView.getLocationOnScreen(curr);
        Log.i("TAG", curr[1] + "");
    }

    @Override
    public void onError() {

    }

    @Override
    public void onExpend() {

    }

    @Override
    public void onShrik() {

    }
    public void displayImage(String url, File saveFile, final GifImageView gifView){
        HttpUtils http = new HttpUtils();
        // 下载图片
        http.download(url, saveFile.getAbsolutePath(), new RequestCallBack<File>() {
            public void onSuccess(ResponseInfo<File> responseInfo) {
                try {
                    Log.e("tag", "onSuccess========" + responseInfo.result.getAbsolutePath());
                    GifDrawable gifFrom = new GifDrawable( responseInfo.result.getAbsolutePath() );
                    gifView.setImageDrawable(gifFrom);
                }
                catch(Exception e){
                    Log.e("tag", e.getMessage());
                }
            }

            @Override
            public void onFailure(com.lidroid.xutils.exception.HttpException e, String s) {
                Log.e("tag", "onFailure========" + s);
            }});
    }
    public File getImageDir(Context context){
        File file = getFilesDir(context, "images");
        return file;
    }
    public File getFilesDir(Context context, String tag){
        if(isSdCardExist() == true){
            return context.getExternalFilesDir(tag);
        }
        else{
            return context.getFilesDir();
        }
    }
    public boolean isSdCardExist() {
        if (Environment.getExternalStorageState().equals(
                Environment.MEDIA_MOUNTED)) {
            return true;
        }
        return false;
    }
    private void closeVideo() {
        adapter=new main_hot_adapter();
        currentPosition = -1;
        isPlaying = false;
        playView.stop();
        adapter.notifyDataSetChanged();
        playView = null;
        currentItemView = null;
    }
    @Override
    public void onScrollStateChanged(AbsListView view, int scrollState) {

    }

    @Override
    public void onScroll(AbsListView view, int firstVisibleItem, int visibleItemCount, int totalItemCount) {
        if ((currentPosition < MainNewListview.getFirstVisiblePosition() || currentPosition > MainNewListview
                .getLastVisiblePosition()) && isPlaying && mcontext.getResources().getConfiguration().orientation
                == Configuration.ORIENTATION_PORTRAIT) {
            closeVideo();
        }
    }

    class MyClick implements View.OnClickListener {
        private int position;
        private VideoPlayView playView;
        private FrameLayout show_layout;
        private View convertView;

        public MyClick(int position, VideoPlayView playView, FrameLayout show_layout, View convertView) {
            this.position = position;
            this.show_layout = show_layout;
            this.playView = playView;
            this.convertView = convertView;
        }

        @Override
        public void onClick(View v) {
            isPlaying = true;
            currentPosition = position;
            show_layout.setVisibility(View.GONE);
            playView.setUrl(listItem.get(position).resource);
            currentItemView = convertView;
            setPlayView(playView);
            playView.openVideo();
            notifyDataSetChanged();
        }
    }
}
